/* MOS                                                        */
/* Giordano Mion, Luca David Opromolla and Alessandro Sforza  */
/* University of Sussex, Banco de Portugal & NC State, and Bologna       */


program drop _all
clear all
capture log close
set more off
local path = "/Users/lucadavidopromolla/Projects/MOS_rev"
log using "`path'/logs/MOS_sample_Apr_29_2022_rev.log", replace
cd "`path'/data"



// Preliminaries (P)
//-----------------

// (P1) Concordance between HS and ISIC Rev. 2
insheet using "`path'/data/Concordance/Concordance_H2_to_I2/JobID-32_Concordance_H2_to_I2.csv",comma clear
keep hs2002productcode isicrevision2productcode
rename hs2002productcode hs6
rename isicrevision2productcode isic4
sort hs6
save "`path'/computation/conc2002",replace
insheet using "`path'/data/Concordance/Concordance_H1_to_I2/JobID-18_Concordance_H1_to_I2.csv",comma clear
keep hs1996productcode isicrevision2productcode
rename hs1996productcode hs6
rename isicrevision2productcode isic4
sort hs6
save "`path'/computation/conc1996",replace
insheet using "`path'/data/Concordance/Concordance_H0_to_I2/JobID-5_Concordance_H0_to_I2.csv",comma clear
keep hs198892productcode isicrevision2productcode
rename hs198892productcode hs6
rename isicrevision2productcode isic4
sort hs6
save "`path'/computation/conc198892",replace


// (P2) Aggregate Rauch product differentiation index at the isic 3-digits level using export shares
do "`path'/codes/MOS_Rauch_Apr_11_2022_rev.do"
// 1. Load Rauch index, at the HS6 level
use "`path'/data/rauch/HS_2002_rauch.dta",clear
keep hs_2002 frac_lib_diff_orig
destring hs_2002,replace
rename hs_2002 hs6
drop if frac_lib_diff_orig==. // Rauch index is missing for 233 out of 4444 hs6 codes
sort hs6
// 2. Attach correspondence between hs6 and isic 4-digits
merge 1:1 hs6 using "`path'/computation/conc2002"
keep if _merge==3 // note that there are no merge==1, so all hs6 codes in Rauch are linked to an isic 4-digit code
drop _merge
sort hs6
// 3. Attach hs6 product shares (pooling all years)
merge 1:1 hs6 using "`path'/computation/P1rauch"
keep if _merge==3 | _merge==1
recode share (.=0)  // a few hs6 codes in Rauch are associated with zero exports in the PT trade data
drop _merge
gen pr_gr=int(isic4/10)
replace pr_gr = 999 if pr_gr!=311 & pr_gr!=313 & pr_gr!=314 & pr_gr!=321 & pr_gr!=322 & pr_gr!=323 & pr_gr!=331 & pr_gr!=332 & pr_gr!=341 & pr_gr!=342 & pr_gr!=351 & pr_gr!=352 & pr_gr!=353 & pr_gr!=354 & pr_gr!=355 & pr_gr!=356 & pr_gr!=361 & pr_gr!=362 & pr_gr!=369 & pr_gr!=371 & pr_gr!=372 & pr_gr!=381 & pr_gr!=382 & pr_gr!=383 & pr_gr!=384 & pr_gr!=385 & pr_gr!=390 & pr_gr!=324 // Aggregate to the 27 Manova's sectors plus Footwear (324) and the residual sector (999)
egen norm_share = pc(share),by(pr_gr) prop
gen double rauch_w = frac_lib_diff_orig*norm_share
collapse (sum) rauch_w,by(pr_gr)
sort pr_gr
save "`path'/computation/rauch",replace


// (P3) Attach concordance between HS and ISIC Rev. 2 to trade data and save trade data
// Prelim
local i = 1991
while `i'<=2006 {
import spss using "`path'/data/QP/QP_Empresas_`i'.sav",clear
cap rename nuemp NUEMP
cap rename nuemp_`j' NUEMP
egen double minemp=min(NUEMP),by(NPC)
egen double maxemp=max(NUEMP),by(NPC)
count if minemp!=maxemp & NPC!=.
drop if minemp!=maxemp & NPC!=.
drop minemp maxemp
egen double minemp=min(NPC),by(NUEMP)
egen double maxemp=max(NPC),by(NUEMP)
count if minemp!=maxemp & NUEMP!=.
drop if minemp!=maxemp & NUEMP!=.
rename NPC_FIC npc_alt
drop if npc_alt==. 
rename NUEMP empresa
rename ano_`j' ano
keep npc_alt ano empresa
if `i'==1991 {
compress
sort npc_alt ano
save "`path'/computation/QPfirm_all_td.dta",replace	
local i = `i' + 1
}
else {
compress
append using "`path'/computation/QPfirm_all_td.dta"
sort npc_alt ano
save "`path'/computation/QPfirm_all_td.dta",replace	
local i = `i' + 1
}
}
use "`path'/computation/Tdata.dta",clear
gen double hs6 = int(nc/100)
sort hs6
preserve
keep if ano>=1991 & ano<=1995
merge m:1 hs6 using "`path'/computation/conc198892"
replace isic4 = 9999 if _merge==1
keep if _merge==3 | _merge==1
drop _merge
save "`path'/computation/Tdata1.dta",replace
restore
preserve
keep if ano>=1996 & ano<=2001
merge m:1 hs6 using "`path'/computation/conc1996"
replace isic4 = 9999 if _merge==1
keep if _merge==3 | _merge==1
drop _merge
save "`path'/computation/Tdata2.dta",replace
restore
preserve
keep if ano>=2002 & ano<=2006
merge m:1 hs6 using "`path'/computation/conc2002"
replace isic4 = 9999 if _merge==1
keep if _merge==3 | _merge==1
drop _merge
save "`path'/computation/Tdata3.dta",replace
restore
use "`path'/computation/Tdata1.dta",clear
append using "`path'/computation/Tdata2.dta"
append using "`path'/computation/Tdata3.dta"
gen double isic3 = int(isic4/10)
replace isic3 = 999 if isic3!=311 & isic3!=313 & isic3!=314 & isic3!=321 & isic3!=322 & isic3!=323 & isic3!=331 & isic3!=332 & isic3!=341 & isic3!=342 & isic3!=351 & isic3!=352 & isic3!=353 & isic3!=354 & isic3!=355 & isic3!=356 & isic3!=361 & isic3!=362 & isic3!=369 & isic3!=371 & isic3!=372 & isic3!=381 & isic3!=382 & isic3!=383 & isic3!=384 & isic3!=385 & isic3!=390 & isic3!=324  // Replace products to a residual category if they do not match those in Manova's dataset or Footwear
drop nc
rename isic3 nc
collapse (sum) v_est,by(npc_alt ano fluxo pais nc)
reshape wide v_est, i(npc_alt ano pais nc) j(fluxo)
save "`path'/computation/P1",replace
erase "`path'/computation/Tdata1.dta"
erase "`path'/computation/Tdata2.dta"
erase "`path'/computation/Tdata3.dta"
// Merge with firm data
merge m:1 npc_alt ano using "`path'/computation/QPfirm_all_td.dta"
tab ano _merge
// Save file to be used to create product groups
preserve
recode v_est* (.=0)
compress
sort npc_alt ano
save "`path'/computation/product_destination",replace
restore
collapse (sum) v_est1 v_est2 (max) empresa,by(npc_alt ano)
recode v_est* (.=0)
compress
sort npc_alt ano
save "`path'/computation/P1",replace


// (P4) Run product-destination dofile
do "`path'/codes/MOS_products_destinations_Apr_11_2022_rev.do"



// Core (C)
//---------


// Loop 1 - merge all years of the firm QP files
local i = 1991
while `i'<=2006 {
	if `i'<2000 {
local j = `i'-1900	
}
else {
local j = `i'-2000
local j  = "0`j'"	
}
import spss using "`path'/data/QP/QP_Empresas_`i'.sav",clear
cap rename nuemp NUEMP
cap rename nuemp_`j' NUEMP
egen double minemp=min(NUEMP),by(NPC)
egen double maxemp=max(NUEMP),by(NPC)
count if minemp!=maxemp & NPC!=.
drop if minemp!=maxemp & NPC!=.
drop minemp maxemp
egen double minemp=min(NPC),by(NUEMP)
egen double maxemp=max(NPC),by(NUEMP)
count if minemp!=maxemp & NUEMP!=.
drop if minemp!=maxemp & NUEMP!=.
rename NPC_FIC npc_alt
drop if npc_alt==. 
rename NUEMP empresa
rename ano_`j' ano
rename n1emp_`j' n1emp
rename n2emp_`j' n2emp
rename cspri_`j' capnacional
rename csest_`j' capestrangeiro
rename cspub_`j' cappublico
rename ancon_`j' anoconst
rename vvend_`j' vendas_euro
rename caem2_`j' nace2
rename caem3_`j' nace3
rename pemp_`j' pemp
keep npc_alt ano n1emp n2emp capnacional capestrangeiro cappublico anoconst vendas_euro nace2 nace3 empresa pemp
if `i'==1991 {
compress
sort empresa ano
save "`path'/computation/QPfirm_all.dta",replace	
local i = `i' + 1
}
else {
compress
append using "`path'/computation/QPfirm_all.dta"
sort empresa ano
save "`path'/computation/QPfirm_all.dta",replace	
local i = `i' + 1
}
}
// Make sales refer to the current year
sort empresa ano
replace vendas_euro = vendas_euro[_n+1] if empresa==empresa[_n+1] & ano==ano[_n+1]-1
sort empresa ano
replace vendas_euro =. if empresa!=empresa[_n+1] | ano!=ano[_n+1]-1
sort empresa ano
save "`path'/computation/QPfirm_all.dta",replace	

// Sector variable consistent through time
gen sector = nace3
tostring sector,replace
gen sector_2d=substr(sector,1,2)
gen sector_3d=substr(sector,1,3)

/**************************************************************************/
/*  Compatibilização dos sectores - códigos da variável sect_comp (31 ct) */
/**************************************************************************/
/*
10  - Agricultura, Produção Animal, Caça e Silvicultura
11  - Pesca
12  - Extracção de Produtos Energéticos
13  - Indústrias Extractivas com Excepção da Extracção de Produtos
Energéticos
20  - Indústrias Alimentares, das Bebidas e do Tabaco
21  - Indústria Têxtil
22  - Indústria do Couro e dos Produtos de Couro
23  - Indústrias da Madeira e da Cortiça e suas Obras
24  - Indústrias de Pasta de Papel e Cartão e seus Artigos, Edição e
Impressão
25 - Fabricação de Coque, Produtos Petrolíferos Refinados e Combustível
Nuclear
26 - Fabricação de Produtos Químicos e de Fibras Sintéticas ou Artificiais
27 - Fabricação de Artigos de Borracha e de Matérias Plásticas
28 - Fabricação de Outros Produtos Minerais Não Metálicos
29 - Indústrias Metalúrgicas de Base e de Prodd_ln_vendas_eurodutos Metálicos
30 - Fabricação de Máquinas e de Equipamentos N.E.
31 - Fabricação de Equipamento Eléctrico e de Óptica
32 - Fabricação de Material de Transporte
33 - Indústrias Transformadoras N.E.
34 - Produção e Distribuição de Electricidade, de Gás e de Água
35 - Construção
40 - Comércio por Grosso e a Retalho, Reparação de Veículos Automóveis,
Motociclos e de Bens de Uso Pessoal e Doméstico
41 - Alojamento e Restauração (Restaurantes e Similares)
42 - Transportes, Armazenagem e Comunicações
43 - Actividades Financeiras
44 - Actividades Imobiliárias, Alugueres e Serviços Prestados às Empresas
50 - Administração Publica, Defesa e Segurança Social Obrigatória
60 - Educação
70 - Saúde e Acção Social
80 - Outras Actividades de Serviços Colectivos, Sociais e Pessoais
90 - Famílias com Empregados Domésticos
91 - Organismos Internacionais e Outras Instituições Extra-territoriais
*/

label define sectors 	10 "Agriculture" ///
						11 "Fishing" ///
						12 "Extracion - energy products" ///
						13 "Extraction - other" ///
						20 "Industry - food drink tobacco" ///
						21 "Industry - textile industry" ///
						22 "Industry - leather industry" ///
						23 "Industry - wood and cork industry" ///
						24 "Industry - paper and print" ///
						25 "Industry - energy products" ///
						26 "Industry - chemistry, fibers" ///
						27 "Industry - plastic" ///
						28 "Industry - minerals not metal" ///
						29 "Industry - metal" ///
						30 "Industry - machines" ///
						31 "Industry - electronics and optics" ///
						32 "Industry - transportation" ///
						33 "Industry - manufacturing, not specified" ///
						34 "Industry - production and distribution of energy" ///
						35 "Industry - construction" ///
						40 "Services - wholesale and retail sale" ///
						41 "Services - hotels and restaurants" ///
						42 "Services - logistics" ///
						43 "Services - finance" ///
						44 "Services - real estate, legal services, IT, communication" ///
						50 "P.A." ///
						60 "Education" ///
						70 "Health" ///
						80 "Other social services, entertainment" ///
						90 "Home workers" ///
						91 "International stuff"
		
gen caem2 = nace2
gen caem3 = nace3		
gen byte sect_comp2	= 0

/*  CAE 73 - de 1985 a 1994 -> sect_comp  */
replace sect_comp2 = 10  if ano<=1994 & (caem2==11 |caem2==12 )
replace sect_comp2 = 11  if ano<=1994 & caem2==13
replace sect_comp2 = 12  if ano<=1994 & (caem2==21 |caem2==22)    
replace sect_comp2 = 13  if ano<=1994 & (caem2==23 | caem3==29)
replace sect_comp2 = 20  if ano<=1994 & (caem2==31)
replace sect_comp2 = 21  if ano<=1994 & (caem2==32)
replace sect_comp2 = 22  if ano<=1994 & (caem3==323 | caem3==324)
replace sect_comp2 = 23  if ano<=1994 & caem2==33
replace sect_comp2 = 24  if ano<=1994 & caem2==34
replace sect_comp2 = 25  if ano<=1994 & (caem3==353 | caem3==354)
replace sect_comp2 = 26  if ano<=1994 & (caem3==351 | caem3==352)
replace sect_comp2 = 27  if ano<=1994 & (caem3==355 | caem3==356)
replace sect_comp2 = 28  if ano<=1994 & caem2==36
replace sect_comp2 = 29  if ano<=1994 & (caem2==37 | caem3==381)
replace sect_comp2 = 30  if ano<=1994 & caem3==382 
replace sect_comp2 = 31  if ano<=1994 & (caem3==383 | caem3==385)
replace sect_comp2 = 32  if ano<=1994 & caem3==384 
replace sect_comp2 = 33  if ano<=1994 & caem3==390
replace sect_comp2 = 34  if ano<=1994 & (caem2==41 | caem2==42)
replace sect_comp2 = 35  if ano<=1994 & caem2==50
replace sect_comp2 = 40  if ano<=1994 & (caem2==61 | caem2==62)
replace sect_comp2 = 41  if ano<=1994 & caem2==63
replace sect_comp2 = 42  if ano<=1994 & caem2==71
replace sect_comp2 = 43  if ano<=1994 & (caem2==81 | caem2==82)
replace sect_comp2 = 44  if ano<=1994 & (caem2==83 | caem2==72)
replace sect_comp2 = 50  if ano<=1994 & (caem3==911 | caem3==912 | caem3==915 | caem3==916 | caem3==917 | caem3==918)
replace sect_comp2 = 60  if ano<=1994 & caem3==913
replace sect_comp2 = 70  if ano<=1994 & caem3==914
replace sect_comp2 = 80  if ano<=1994 & (caem2==80 | caem2==93 | caem2==94 | caem3==951 | caem3==952 | caem3==959)
replace sect_comp2 = 90  if ano<=1994 & caem3==953
replace sect_comp2 = 91  if ano<=1994 & caem3==960

drop if (ano<=1994 & (caem3==911 | caem3==912 | caem3==915 | caem3==916 | caem3==917 | caem3==918)) | (ano<=1994 & caem3==913) | (ano<=1994 & caem3==914) | (ano<=1994 & caem3==960) //ALE//

 /*  Rev2 -> sect_comp (1995-2006)  */
replace sect_comp2 = 10  if ano>=1995 & ano<2007 & (caem2==01 | caem2==02)
replace sect_comp2 = 11  if ano>=1995 & ano<2007 & caem2==05
replace sect_comp2 = 12  if ano>=1995 & ano<2007 & (caem2==10 | caem2==11| caem2==12)
replace sect_comp2 = 13 if ano>=1995 & ano<2007 & (caem2==13 | caem2==14)
replace sect_comp2 = 20  if ano>=1995 & ano<2007 & (caem2==15 | caem2==16)
replace sect_comp2 = 21  if ano>=1995 & ano<2007 & (caem2==17 | caem2==18)
replace sect_comp2 = 22  if ano>=1995 & ano<2007 & caem2==19
replace sect_comp2 = 23  if ano>=1995 & ano<2007 & caem2==20
replace sect_comp2 = 24 if ano>=1995 & ano<2007 & (caem2==21 | caem2==22)
replace sect_comp2 = 25 if ano>=1995 & ano<2007 & caem2==23
replace sect_comp2 = 26 if ano>=1995 & ano<2007 & caem2==24
replace sect_comp2 = 27 if ano>=1995 & ano<2007 & caem2==25
replace sect_comp2 = 28 if ano>=1995 & ano<2007 & caem2==26
replace sect_comp2 = 29 if ano>=1995 & ano<2007 & (caem2==27 | caem2==28)
replace sect_comp2 = 30 if ano>=1995 & ano<2007 & caem2==29
replace sect_comp2 = 31 if ano>=1995 & ano<2007 & (caem2==30 | caem2==31| caem2==32 | caem2==33)
replace sect_comp2 = 32 if ano>=1995 & ano<2007 & (caem2==34 | caem2==35)
replace sect_comp2 = 33 if ano>=1995 & ano<2007 & (caem2==36 | caem2==37)
replace sect_comp2 = 34 if ano>=1995 & ano<2007 & (caem2==40 | caem2==41)
replace sect_comp2 = 35 if ano>=1995 & ano<2007 & caem2==45
replace sect_comp2 = 40 if ano>=1995 & ano<2007 & (caem2==50 | caem2==51| caem2==52)
replace sect_comp2 = 41 if ano>=1995 & ano<2007 & caem2==55
replace sect_comp2 = 42 if ano>=1995 & ano<2007 & (caem2==60 | caem2==61| caem2==62 | caem2==63 | caem2==64)
replace sect_comp2 = 43 if ano>=1995 & ano<2007 & (caem2==65 | caem2==66| caem2==67)
replace sect_comp2 = 44 if ano>=1995 & ano<2007 & (caem2==70 | caem2==71| caem2==72 | caem2==73 | caem2==74)
replace sect_comp2 = 50 if ano>=1995 & ano<2007 & caem2==75
replace sect_comp2 = 60 if ano>=1995 & ano<2007 & caem2==80
replace sect_comp2 = 70 if ano>=1995 & ano<2007 & caem2==85
replace sect_comp2 = 80 if ano>=1995 & ano<2007 & (caem2==90 | caem2==91| caem2==92 | caem2==93)
replace sect_comp2 = 90 if ano>=1995 & ano<2007 & caem2==95
replace sect_comp2 = 91 if ano>=1995 & ano<2007 & caem2==99

drop if (ano>=1995 & ano<2007 & caem2==75) | (ano>=1995 & ano<2007 & caem2==99) //ALE//

/*  Rev3 -> sect_comp (2007)  */
replace sect_comp2 = 10  if ano>=2007 & (caem2==1 |caem2==2| caem3==813)
replace sect_comp2 = 11  if ano>=2007 & caem2==3
replace sect_comp2 = 12  if ano>=2007 & (caem2==5 |caem2==6| caem3==72)    //does not exist?!
replace sect_comp2 = 13 if ano>=2007 & ((caem2==7 & caem3!=72) | caem2==8 | caem2==9)
replace sect_comp2 = 20  if ano>=2007 & (caem2==10 | caem2==11| caem2==12 | caem3==201)
replace sect_comp2 = 21  if ano>=2007 & (caem2==13 | caem2==14| caem3==151)
replace sect_comp2 = 22  if ano>=2007 & ((caem2==15 & caem3!=151) | caem3==221 | caem3==222)
replace sect_comp2 = 23  if ano>=2007 & (caem2==16 |caem3==329)
replace sect_comp2 = 24  if ano>=2007 & (caem2==17 | caem2==18| caem2==58)
replace sect_comp2 = 25 if ano>=2007 & (caem2==19 |caem3==244)
replace sect_comp2 = 26 if ano>=2007 & ((caem2==20 & caem3!=201) | caem2==21 | caem3==268)
replace sect_comp2 = 27 if ano>=2007 & (caem2==22 & caem3!=222& caem3!=221)
replace sect_comp2 = 28 if ano>=2007 & caem2==23
replace sect_comp2 = 29 if ano>=2007 & ((caem2==24 & caem3!=244) | (caem2==25 & caem3!=257 & caem3!=254 &caem3!=254))
replace sect_comp2 = 30 if ano>=2007 & (caem2==28 | (caem2==33 & caem3!=331 & caem3!=332) | caem3==257 |caem3==254 | caem3==304 | caem3==275 |caem3==275)
replace sect_comp2 = 31 if ano>=2007 & ((caem2==26 & caem3!=268) | (caem2==27 & caem3!=275) |caem3==293 | caem3==325 | caem3==332)
replace sect_comp2 = 32 if ano>=2007 & ((caem2==29 & caem3!=293) | (caem2==30 & caem3!=304) | caem3==331)
replace sect_comp2 = 33 if ano>=2007 & (caem2==31 | (caem2==32 & caem3!=329 & caem3!=325) | caem3==383)
replace sect_comp2 = 34 if ano>=2007 & (caem2==35 | caem2==36)
replace sect_comp2 = 35 if ano>=2007 & (caem2==41 | caem2==42| caem2==43)
replace sect_comp2 = 40 if ano>=2007 & (caem2==45 | caem2==46| caem2==47 | caem2==95)
replace sect_comp2 = 41 if ano>=2007 & (caem2==55 | caem2==56)
replace sect_comp2 = 42 if ano>=2007 & (caem2==49 | caem2==50| caem2==51 | caem2==52 | caem2==53 | caem3==791 |caem2==61)
replace sect_comp2 = 43 if ano>=2007 & (caem2==64 | caem2==65| caem2==66)
replace sect_comp2 = 44 if ano>=2007 & (caem2==60 | caem2==62| caem2==63 | caem2==68 | caem2==69 | caem2==70 |caem2==71 | caem2==72 | caem2==73 | caem2==74 |caem2==77 | caem2==78 | (caem2==79 & caem3!=791) |caem2==80 | (caem2==81 & caem3!=813) | caem2==82)
replace sect_comp2 = 50 if ano>=2007 & caem2==84
replace sect_comp2 = 60 if ano>=2007 & caem2==85
replace sect_comp2 = 70 if ano>=2007 & (caem2==75 | caem2==86| caem2==87 | caem2==88)
replace sect_comp2 = 80 if ano>=2007 & (caem2==37 | (caem2==38 & caem3!=383) | caem2==39 | caem2==59 |caem2==90 | caem2==91 | caem2==92 | caem2==93 |caem2==94 | caem2==96)
replace sect_comp2 = 90 if ano>=2007 & caem2==97
replace sect_comp2 = 91 if ano>=2007 & caem2==99

drop if (ano>=2007 & caem2==84) | (ano>=2007 & caem2==99) //ALE//

label values sect_comp2 sectors
label var sect_comp2 "firm 2 digit sector, consistent"
drop if sect_comp2==0
drop sector
rename sect_comp2 sector
sort empresa ano
save "`path'/computation/QPfirm_all.dta",replace



// Loop 2 - attach firm-level info (from QP and trade) to worker-level
// Note that we skip 2001 since QP workers' module for 2001 does not exist.

local i = 1991
while `i'<=2006 {

// File with firm-level information
use "`path'/computation/QPfirm_all.dta",clear
keep if ano==`i'
sort empresa ano
save "`path'/computation/QPfirm`i'.dta",replace

// File with worker-level information
import spss using "`path'/data/QP/QP_Trabalhadores_`i'.sav",clear
recast double ntrab
format ntrab %16.0f
replace ntrab = floor(ntrab)
if `i'<2000 {
local j = `i'-1900	
}
else {
local j = `i'-2000
local j  = "0`j'"	
}

rename ntrab_`j' nss
drop if nss==0
rename ano_`j' ano
rename nuemp_`j' empresa
rename sexo_`j' sexo
rename nqual_`j' qualif
rename ctrem_`j' crem
gen anoadm=int(dtadm_`j'/100)
rename nhnor_`j' hnormais
rename nhext_`j' hextra
rename habil_`j' hab
rename idade_`j'_TB_COD age
rename rbase_`j' rembase
rename rextr_`j' remhextra
rename rprg_`j' subsidios
rename prof_4d`j' prof // note: this is CNP80 from 1991 until 1994 included, and CNP94 from 1995 to 2006
keep nss ano empresa sexo qualif crem anoadm hnormais hextra hab age rembase remhextra subsidios prof

// Attach QP firm-level information to worker-level
sort empresa ano
merge empresa ano using "`path'/computation/QPfirm`i'.dta"
tab _merge
keep if _merge==3
drop _merge
erase "`path'/computation/QPfirm`i'.dta"

// Attach trade firm-level information to worker-level
preserve
use "`path'/computation/P1.dta",clear
keep if ano==`i'
sort npc_alt ano
save "`path'/computation/trade.dta",replace
restore
sort npc_alt ano
merge npc_alt ano using "`path'/computation/trade.dta"
tab _merge
keep if _merge==3 | _merge==1
recode v_est* (.=0)
drop _merge

// Selection according to firms' QP information
keep if n1emp==1  /* mainland */
drop if nace2<10 | nace2>=95 /* drop agriculture, fishering and other minor */
replace vendas_euro = .  if vendas_euro<=0
replace anoconst = . if anoconst<1600 
gen double ln_firmage = log(ano-anoconst+1) if ano>=anoconst & anoconst!=.
replace capnacional =. if capnacional<0 | capnacional>100  /* check on ownership variables: replace to missing if share not in 0-100 */
replace capestrangeiro =. if capestrangeiro<0 | capestrangeiro>100
replace cappublico =. if cappublico<0 | cappublico>100
gen double checkcap = capnacional+ capestrangeiro+ cappublico
replace capnacional =. if checkcap>100 | (checkcap>0 & checkcap<95) /* check on ownership variables: replace to missing if sum of shares not in 0-100 */
replace capestrangeiro =. if checkcap>100 | (checkcap>0 & checkcap<95)
replace cappublico =. if checkcap>100 | (checkcap>0 & checkcap<95)
drop checkcap
gen double checkcap = capnacional+ capestrangeiro+ cappublico
gen double foreign_loose = capestrangeiro>50  /* Foreign Dummy: note that here we assume a firm is not foreign if we do not have info on capital ownership */
replace foreign_loose = . if  capestrangeiro==.
gen double foreign = foreign_loose if checkcap!=0 & checkcap!=.     /* Foreign Dummy */
drop checkcap capnacional cappublico capestrangeiro foreign_loose

// Selection according to workers' characteristics
// Recoding
replace hab = . if hab<110 | hab>=999
replace qualif = . if qualif<10 | qualif>84
replace anoadm = . if anoadm<ano-100 | anoadm>ano
replace hnormais = 0 if hnormais<0
replace hnormais = . if hnormais>480 //480 = 30*16
replace hextra = 0 if hextra<0
replace hextra = . if hextra>480 //480 = 30*16
tab crem
keep if crem==0 /* full-time */
drop crem
replace sexo = 0 if sexo ==1 /* males */
replace sexo = 1 if sexo ==2 /* females */
format sexo* %4.2f
rename sexo fem
keep if age>=16 & age<=65
label var age "age at the end of the yr"
tab age
gen double tot_hours_wk = (hnormais + hextra)/4.3333333 // Work >= 25 & <=80 hours per week
keep if tot_hours_wk>=25 & tot_hours_wk<=80
label var tot_hours_wk "tot hours per week"
egen double jobs = count(ano),by(nss) // Keep only workers with one job
tab jobs
drop if jobs>1
gen double educa = int(hab/100) // Education in years
gen double educa2 = int(hab/10)
gen double educ_yrs = . // LUCA
if `i'>=1986 & `i'<=1993 {
 drop if hab<110 | hab>=999
 replace educ_yrs = 0 if hab == 110 /*inferior 1 ciclo */
 replace educ_yrs = 3 if hab == 120 /*inferior 1 ciclo */
 replace educ_yrs = 4 if hab == 210 /*1 ciclo */
 replace educ_yrs = 6 if hab == 310 /*2 ciclo */
 replace educ_yrs = 9 if hab == 410 /*3 ciclo */
 replace educ_yrs = 12 if hab>=420 & hab<=520 /*secundario*/
 replace educ_yrs = 16 if hab>=699 & hab<=799 /*bach_licenc*/
 }
else if `i'>=1994 & `i'<=2005 {
 drop if hab<110
 replace educ_yrs = 0 if hab == 110 /*inferior 1 ciclo */
 replace educ_yrs = 3 if hab == 120 /*inferior 1 ciclo */
 replace educ_yrs = 4 if educa == 2 /*1 ciclo */ //ALE//
 replace educ_yrs = 6 if educa == 3 /*2 ciclo */ //ALE//
 //replace educ_yrs = 4 if hab == 210 /*1 ciclo */ //ALE//
 //replace educ_yrs = 6 if hab == 310 /*2 ciclo */ //ALE//
 replace educ_yrs = 9 if educa == 4 & hab!=420 /*3 ciclo */ //ALE//
 replace educ_yrs = 12 if educa == 5 | hab == 420 /*secundario*/ //ALE//
 replace educ_yrs = 16 if (educa == 6 | educa == 7) /*bach_licenc*/
 }
 else {
 drop if hab<110
 replace educ_yrs = 0 if hab == 111 /*inferior 1 ciclo */
 replace educ_yrs = 3 if hab == 112 /*inferior 1 ciclo */
 replace educ_yrs = 4 if educa2 == 21 /*1 ciclo */ //ALE//
 replace educ_yrs = 6 if educa2 == 22 /*2 ciclo */ //ALE//
 //replace educ_yrs = 4 if hab == 211 /*1 ciclo */ //ALE//
 //replace educ_yrs = 6 if hab == 221 /*2 ciclo */ //ALE//
 replace educ_yrs = 9 if educa2 == 23 /*3 ciclo */
 replace educ_yrs = 12 if (educa2 == 31 | educa == 4) /*secundario*/
 replace educ_yrs = 16 if educa2 >= 50 & educa2 <= 80 /*bach_licenc*/
 }
drop educa hab
label var educ_yrs "yrs of education"
// (log) Hourly wage
gen double hwage = log((rembase+subsidios+remhextra)/(tot_hours_wk*4.3333333))
gen double hwage_norm = log((rembase+subsidios+remhextra)/(hnormais))
drop hnormais hextra rembase subsidios remhextra
sort empresa
// Trimming 
egen double trim_bt = pctile(hwage),p(.5)
egen double trim_up = pctile(hwage),p(99.5)
drop if hwage<trim_bt | (hwage>trim_up & hwage!=.)
drop trim*
// Drop workers that appear more than once in any year
egen double check=count(ano),by(nss)  
sort nss
gen id1= nss!=nss[_n-1]
tab check if id1==1
keep if check==1
drop check id1
// Save
if `i'==1991 {
compress
sort empresa ano
save "`path'/computation/C1.dta",replace	
local i = `i' + 1
}
else if `i'!=2000 {
compress
append using "`path'/computation/C1.dta"
sort empresa ano
save "`path'/computation/C1.dta",replace	
local i = `i' + 1
}
else if `i'==2000 {
compress
append using "`path'/computation/C1.dta"
sort empresa ano
save "`path'/computation/C1.dta",replace	
local i = 2002
}

}


// (C5) Create robust anoadm variable (take mode for a worker-empresa pair; if there is a tie take the min; drop if anoadm is still missing) and tenure variable
sort nss ano
gen double anoadm_group = 1 if nss!=nss[_n-1] | empresa!=empresa[_n-1]
recode anoadm_group .=0
sort nss ano
replace anoadm_group =anoadm_group+anoadm_group[_n-1] if _n!=1
rename anoadm anoadm_raw
egen double anoadm = mode(anoadm_raw),minmode by(anoadm_group)
drop if anoadm==.
gen anoadm_group2 = anoadm_group if anoadm==anoadm_raw // within anoadm_group we select those obs whose anoadm equals the mode; using these, we will then take the mode of mesadm
egen double anoadm_min = min(ano),by(anoadm_group)
replace anoadm = anoadm_min if anoadm>anoadm_min
drop anoadm_min anoadm_group anoadm_group2
// Tenure 
gen double ten = ano - anoadm   
replace ten =. if ten<0
tab ten
compress
save "`path'/computation/C5.dta",replace


// (C6) Define manager, manager type and share of skilled workers
//Manager
gen manager = int(qualif/10)>=1 & int(qualif/10)<=3
replace manager = . if qualif==.
gen manager_type = 1 if manager==1 & ano>=1995 & ano<=2006
replace manager_type= 2 if (prof=="1233" & manager==1 & ano>=1995 & ano<=2006)
replace manager_type = . if qualif==.
tab manager if ano>=1995
tab manager manager_type
gen byte mant1 = manager_type==1
gen byte mant2 = manager_type==2
label var mant1 "Other Manager"
label var mant2 "Sales Manager"
//Skilled worker
drop qualif
gen skilled = educ_yrs>=12 & educ_yrs!=.
replace skilled = . if educ_yrs==.
egen double sh_skilled = mean(skilled),by(empresa ano)
gen sh_skilled_d = sh_skilled!=.
recode sh_skilled (.=0)
compress
sort empresa ano
save "`path'/computation/C6.dta",replace


// (C7) Create Trade status dummies (new exporter, continuing exporter...)
preserve
use "`path'/computation/P1.dta",clear
keep if ano==2001
drop v_est1
collapse (mean) v_est2,by(empresa ano)
save "`path'/computation/trade2001.dta",replace
restore
preserve
collapse (mean) v_est2,by(empresa ano)
append using "`path'/computation/trade2001.dta"
gen exporting = v_est2>0 & v_est2!=.
replace exporting = . if v_est2==.
xtset empresa ano                                                                   
gen byte new_exporter = exporting==1 & l1.exporting==0
gen byte cont_exporter = exporting==1 & l1.exporting==1
gen byte exit_exporter = exporting==0 & l1.exporting==1
gen byte never_exporter = exporting==0 & l1.exporting==0
gen byte other_exporter = 1 - new_exporter - cont_exporter - exit_exporter - never_exporter
drop v_est2
sort empresa ano
save "`path'/computation/trade_status.dta",replace
restore
sort empresa ano
merge empresa ano using "`path'/computation/trade_status.dta"
tab ano _merge
keep if _merge==3 /* _merge==2 are the 2001 firms */
drop _merge
sort nss ano
save "`path'/computation/C7.dta",replace


// (C8) Fill in empresa variable using information from "anoadm" variable (we do this because we miss 2001 in the workers file)
preserve
keep nss ano empresa anoadm npc_alt
keep if ano==2002 | ano==2003
replace ano = 2001 if ano==2002 & anoadm<=2001
drop if ano==2002
egen double done = min(ano),by(nss)
drop if done==2001 & ano>2001
drop done
replace ano = 2001 if ano==2003 & anoadm<=2001
drop if ano==2003
egen double done = min(ano),by(nss)
drop done
sort empresa ano
save "`path'/computation/fill2001.dta",replace
sum
use "`path'/computation/P1.dta",clear
keep if ano==2001
sort empresa ano
save "`path'/computation/trade2001.dta",replace
use "`path'/computation/fill2001.dta",clear
merge empresa ano using "`path'/computation/trade2001.dta"
tab _merge
keep if _merge==3
drop _merge
keep nss ano empresa anoadm v_est2
save "`path'/computation/filled_workers.dta",replace
restore
append using "`path'/computation/filled_workers.dta"
tab ano
save "`path'/computation/C8.dta",replace  

use "`path'/computation/destinations1_ap",clear
sort empresa ano
save "`path'/computation/destinations1_ap_e",replace


// (C9) Construct workers' export experience in a destination
use "`path'/computation/C8.dta",clear
preserve
drop exporting
// Insert data set on firms' export destinations generated with the dofile destinations.do
sort empresa ano
merge empresa ano using "`path'/computation/destinations1_ap_e.dta"
drop exporting
tab ano _merge
keep if _merge==1 | _merge==3
drop _merge
gen exporting = v_est2>0 & v_est2!=. // need to compute exporting for 2001 as well
replace exporting = . if v_est2==.
keep ano empresa nss exporting de_*
compress
reshape wide empresa exporting de_*,i(nss) j(ano) 

// Split in three to ease computation burden
sort nss
xtile split = nss,nq(5)
save "`path'/computation/split.dta",replace

forvalues o=1/5 {
	use "`path'/computation/split.dta",clear
	keep if split==`o'

local v=1992 
while `v'<=2006 {
local d = 1
while `d'<=`v'-1991 {
local s = `v' - `d'
if `d'==1 {
gen byte iexp`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1   /* dummy 0/1 : worked in other firms, that were exporting, in the past */
gen byte iexp_spain`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_spain`s'==1
gen byte iexp_ooecd`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ooecd`s'==1
gen byte iexp_china`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_china`s'==1
gen byte iexp_otop5`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_otop5`s'==1
gen byte iexp_excol`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_excol`s'==1
gen byte iexp_oeu`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_oeu`s'==1
gen byte iexp_row`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_row`s'==1
gen byte iexp_ang`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`s'==1
gen byte iexp_cv`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_cv`s'==1
gen byte iexp_gw`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_gw`s'==1
gen byte iexp_st`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_st`s'==1
gen byte iexp_mz`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_mz`s'==1
gen byte iexp_br`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_br`s'==1
gen byte iexp_tl`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_tl`s'==1

gen byte yriexp`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1   /* dummy 0/1 : worked in other firms, that were exporting, in the past */
gen byte yriexp_spain`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_spain`s'==1
gen byte yriexp_ooecd`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ooecd`s'==1
gen byte yriexp_china`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_china`s'==1
gen byte yriexp_otop5`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_otop5`s'==1
gen byte yriexp_excol`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_excol`s'==1
gen byte yriexp_oeu`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_oeu`s'==1
gen byte yriexp_row`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_row`s'==1
gen byte yriexp_ang`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`s'==1
gen byte yriexp_cv`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_cv`s'==1
gen byte yriexp_gw`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_gw`s'==1
gen byte yriexp_st`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_st`s'==1
gen byte yriexp_mz`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_mz`s'==1
gen byte yriexp_br`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_br`s'==1
gen byte yriexp_tl`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_tl`s'==1

local pc  = "311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach h of local pc {
gen byte iexp_ang`h'`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`h'`s'==1
gen byte yriexp_ang`h'`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`h'`s'==1
}
}
else {
replace iexp`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1
replace iexp_spain`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_spain`s'==1
replace iexp_ooecd`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ooecd`s'==1
replace iexp_china`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_china`s'==1
replace iexp_otop5`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_otop5`s'==1
replace iexp_excol`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_excol`s'==1
replace iexp_oeu`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_oeu`s'==1
replace iexp_row`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_row`s'==1
replace iexp_ang`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`s'==1
replace iexp_cv`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_cv`s'==1
replace iexp_gw`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_gw`s'==1
replace iexp_st`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_st`s'==1
replace iexp_mz`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_mz`s'==1
replace iexp_br`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_br`s'==1
replace iexp_tl`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_tl`s'==1

replace yriexp`v' = `d' if yriexp`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1
replace yriexp_spain`v' = `d' if yriexp_spain`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_spain`s'==1
replace yriexp_ooecd`v' = `d' if yriexp_ooecd`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ooecd`s'==1
replace yriexp_china`v' = `d' if yriexp_china`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_china`s'==1
replace yriexp_otop5`v' = `d' if yriexp_otop5`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_otop5`s'==1
replace yriexp_excol`v' = `d' if yriexp_excol`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_excol`s'==1
replace yriexp_oeu`v' = `d' if yriexp_oeu`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_oeu`s'==1
replace yriexp_row`v' = `d' if yriexp_row`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_row`s'==1
replace yriexp_ang`v' = `d' if yriexp_ang`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`s'==1
replace yriexp_cv`v' = `d' if yriexp_cv`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_cv`s'==1
replace yriexp_gw`v' = `d' if yriexp_gw`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_gw`s'==1
replace yriexp_st`v' = `d' if yriexp_st`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_st`s'==1
replace yriexp_mz`v' = `d' if yriexp_mz`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_mz`s'==1
replace yriexp_br`v' = `d' if yriexp_br`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_br`s'==1
replace yriexp_tl`v' = `d' if yriexp_tl`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_tl`s'==1


local pc  = "311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach h of local pc {
replace iexp_ang`h'`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`h'`s'==1
replace yriexp_ang`h'`v' = `d' if yriexp_ang`h'`v'==. & empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & de_ang`h'`s'==1
}
}
local d = `d' +1
}
local v = `v' +1
}

save "`path'/computation/split`o'.dta",replace
}

forvalues u=1/5 {
use "`path'/computation/split`u'.dta",clear
drop exporting* de_*
reshape long empresa iexp iexp_spain iexp_ooecd iexp_china iexp_otop5 iexp_excol iexp_oeu iexp_row iexp_ang iexp_cv iexp_gw iexp_st iexp_mz iexp_br iexp_tl iexp_ang311 iexp_ang313 iexp_ang314 iexp_ang321 iexp_ang322 iexp_ang323 iexp_ang324 iexp_ang331 iexp_ang332 iexp_ang341 iexp_ang342 iexp_ang351 iexp_ang352 iexp_ang353 iexp_ang354 iexp_ang355 iexp_ang356 iexp_ang361 iexp_ang362 iexp_ang369 iexp_ang371 iexp_ang372 iexp_ang381 iexp_ang382 iexp_ang383 iexp_ang384 iexp_ang385 iexp_ang390 iexp_ang999 yriexp yriexp_spain yriexp_ooecd yriexp_china yriexp_otop5 yriexp_excol yriexp_oeu yriexp_row yriexp_ang yriexp_cv yriexp_gw yriexp_st yriexp_mz yriexp_br yriexp_tl yriexp_ang311 yriexp_ang313 yriexp_ang314 yriexp_ang321 yriexp_ang322 yriexp_ang323 yriexp_ang324 yriexp_ang331 yriexp_ang332 yriexp_ang341 yriexp_ang342 yriexp_ang351 yriexp_ang352 yriexp_ang353 yriexp_ang354 yriexp_ang355 yriexp_ang356 yriexp_ang361 yriexp_ang362 yriexp_ang369 yriexp_ang371 yriexp_ang372 yriexp_ang381 yriexp_ang382 yriexp_ang383 yriexp_ang384 yriexp_ang385 yriexp_ang390 yriexp_ang999,i(nss) j(ano)
drop if empresa==.
save "`path'/computation/split`u'.dta",replace
}

use "`path'/computation/split1.dta",clear
forvalues u=2/5 {
append using "`path'/computation/split`u'.dta"
}
save "`path'/computation/split_final.dta",replace
save "`path'/computation/temp_loop.dta",replace


// Fix issue of workers returning to old firms
gen double minexpano = ano if iexp==1
egen double minexp=min(minexpano),by(nss)
gen exp = (ano>=minexp)
gen yrexp = yriexp if ano>=minexp
sort nss ano
replace yrexp = yrexp[_n-1] + 1 if yrexp==. & ano>=minexp
local dest "spain ooecd china otop5 excol oeu row ang cv gw st mz"
foreach x of local dest {
di "`x'"
gen double minexpano_`x' = ano if iexp_`x'==1
egen double minexp_`x'=min(minexpano_`x'),by(nss)
gen exp_`x' = (ano>=minexp_`x')
gen yrexp_`x' = yriexp_`x' if ano>=minexp_`x'
sort nss ano
replace yrexp_`x' = yrexp_`x'[_n-1] + 1 if yrexp_`x'==. & ano>=minexp_`x'
drop iexp_`x' minexp_`x' minexpano_`x' yriexp_`x'
replace exp_`x' =. if exp_`x'==0 & ano==1991
replace yrexp_`x' =. if yrexp_`x'==0 & ano==1991
}
local dest "br tl ang311 ang313 ang314 ang321 ang322 ang323 ang324 ang331"
foreach x of local dest {
di "`x'"
gen double minexpano_`x' = ano if iexp_`x'==1
egen double minexp_`x'=min(minexpano_`x'),by(nss)
gen exp_`x' = (ano>=minexp_`x')
gen yrexp_`x' = yriexp_`x' if ano>=minexp_`x'
sort nss ano
replace yrexp_`x' = yrexp_`x'[_n-1] + 1 if yrexp_`x'==. & ano>=minexp_`x'
drop iexp_`x' minexp_`x' minexpano_`x' yriexp_`x'
replace exp_`x' =. if exp_`x'==0 & ano==1991
replace yrexp_`x' =. if yrexp_`x'==0 & ano==1991
}
local dest "ang332 ang341 ang342 ang351 ang352 ang353 ang354 ang355 ang356"
foreach x of local dest {
di "`x'"
gen double minexpano_`x' = ano if iexp_`x'==1
egen double minexp_`x'=min(minexpano_`x'),by(nss)
gen exp_`x' = (ano>=minexp_`x')
gen yrexp_`x' = yriexp_`x' if ano>=minexp_`x'
sort nss ano
replace yrexp_`x' = yrexp_`x'[_n-1] + 1 if yrexp_`x'==. & ano>=minexp_`x'
drop iexp_`x' minexp_`x' minexpano_`x' yriexp_`x'
replace exp_`x' =. if exp_`x'==0 & ano==1991
replace yrexp_`x' =. if yrexp_`x'==0 & ano==1991
}
local dest "ang361 ang362 ang369 ang371 ang372 ang381 ang382 ang383 ang384 ang385 ang390 ang999"
foreach x of local dest {
di "`x'"
gen double minexpano_`x' = ano if iexp_`x'==1
egen double minexp_`x'=min(minexpano_`x'),by(nss)
gen exp_`x' = (ano>=minexp_`x')
gen yrexp_`x' = yriexp_`x' if ano>=minexp_`x'
sort nss ano
replace yrexp_`x' = yrexp_`x'[_n-1] + 1 if yrexp_`x'==. & ano>=minexp_`x'
drop iexp_`x' minexp_`x' minexpano_`x' yriexp_`x'
replace exp_`x' =. if exp_`x'==0 & ano==1991
replace yrexp_`x' =. if yrexp_`x'==0 & ano==1991
}
drop iexp* minexp* yriexp*
replace exp =. if exp==0 & ano==1991
replace yrexp =. if yrexp==0 & ano==1991
compress
sort nss ano
save "`path'/computation/experience.dta",replace
restore
sort nss ano
merge nss ano using "`path'/computation/experience.dta"
tab _merge
keep if _merge==3
drop _merge
drop if ano==2001 /* drop filled workers */
// Interact manager type with export experience
gen byte mante1 = manager_type==1 & exp==1
gen byte mante2 = manager_type==2 & exp==1
recode mante1 mante2 (0=.) if (exp==. | manager==.)
label var mante1 "Other Manager with experience"
label var mante2 "Sales Manager with experience"
sort empresa ano
save "`path'/computation/C9.dta",replace


// (C10) Size, productivity, and sector of the worker's previous firm
gen double relsize = log(pemp)
gen double relapp_lbprod = log(vendas_euro/pemp)
sort nss ano
gen double prev_firmid = empresa[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=.
sort nss ano
replace prev_firmid = prev_firmid[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] & prev_firmid==.  
sort nss ano
gen double prev_size = relsize[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=.
sort nss ano
replace prev_size = prev_size[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] & prev_size==. 
sort nss ano
gen double prev_prod = relapp_lbprod[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=.
sort nss ano
replace prev_prod = prev_prod[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] &  prev_prod==.
sort nss ano
gen double prev_secs = nace2==nace2[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=. & nace2!=. & nace2[_n-1]!=.
sort nss ano
replace prev_secs = prev_secs[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] &  prev_secs==.
sort nss ano
gen double prev_secd = nace2!=nace2[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=. & nace2!=. & nace2[_n-1]!=.
sort nss ano
replace prev_secd = prev_secd[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] &  prev_secd==.
recode prev_secs prev_secd (.=0)
gen prev_sec_d = prev_secs==0 & prev_secd==0  // = 1 if missing
sum nss ano prev_size prev_prod prev_sec*
gen prev_size_d = prev_size!=.  // = 1 if non-missing
gen prev_prod_d = prev_prod!=.  // = 1 if non-missing
recode prev_size prev_prod (.=0) 
drop relsize relapp_lbprod
sum nss ano prev_size* prev_prod* prev_sec*
save "`path'/computation/C10.dta",replace 



// Create Variables to be Used in the Regressions and Save (V)
//------------------------------------------------------------

// (V2) Matched export experience
// matchexp = 1 if the worker has experience in a destination the firm is exporting to
keep ano empresa fem pemp vendas_euro v_est2 nace2 ln_firmage foreign sh_skilled sh_skilled_d nace2 nss age educ_yrs hwage hwage_norm anoadm ten manager*  new_exporter cont_exporter exit_exporter never_exporter other_exporter yrexp* exp* prev_size prev_prod prev_secs prev_secd prev_size_d prev_prod_d prev_sec_d prev_firmid tot_hours_wk n2emp npc_alt mant*
sort npc_alt ano
merge npc_alt ano using "`path'/computation/destinations1_ap"
tab ano _merge
keep if _merge==1 | _merge==3
drop _merge
gen byte matchexp = (de_spain==1 & exp_spain==1) | (de_ooecd==1 & exp_ooecd==1) | (de_china==1 & exp_china==1) |  (de_otop5==1 & exp_otop5==1) | (de_excol==1 & exp_excol==1) | (de_oeu==1 & exp_oeu==1) | (de_row==1 & exp_row==1) | (de_ang==1 & exp_ang==1) | (de_cv==1 & exp_cv==1) | (de_gw==1 & exp_gw==1) | (de_st==1 & exp_st==1) | (de_mz==1 & exp_mz==1) | (de_br==1 & exp_br==1) | (de_tl==1 & exp_tl==1)
replace matchexp = . if exp==.
gen byte matchexp_ang = (de_ang==1 & exp_ang==1) 
replace matchexp_ang = . if exp==.
rename de_spain de_1
rename de_ooecd de_2
rename de_china de_3
rename de_otop5 de_4
rename de_excol de_5
rename de_oeu de_6
rename de_row de_7
rename de_ang de_8
rename de_cv de_9
rename de_gw de_10
rename de_st de_11
rename de_mz de_12
rename de_br de_13
rename de_tl de_14
rename exp_spain exp_1
rename exp_ooecd exp_2
rename exp_china exp_3
rename exp_otop5 exp_4
rename exp_excol exp_5
rename exp_oeu exp_6
rename exp_row exp_7
rename exp_ang exp_8
rename exp_cv exp_9
rename exp_gw exp_10
rename exp_st exp_11
rename exp_mz exp_12
rename exp_br exp_13
rename exp_tl exp_14
rename yrexp_spain yrexp_1
rename yrexp_ooecd yrexp_2
rename yrexp_china yrexp_3
rename yrexp_otop5 yrexp_4
rename yrexp_excol yrexp_5
rename yrexp_oeu yrexp_6
rename yrexp_row yrexp_7
rename yrexp_ang yrexp_8
rename yrexp_cv yrexp_9
rename yrexp_gw yrexp_10
rename yrexp_st yrexp_11
rename yrexp_mz yrexp_12
rename yrexp_br yrexp_13
rename yrexp_tl yrexp_14
local pc  = "311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach h of local pc {
rename de_ang`h' de_`h'
rename exp_ang`h' exp_`h'
}
// Interact manager type "i" with export experience in destination group "j"
forvalues i=1/2 {
local pc  = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach j of local pc {
gen byte mante`i'_`j' = manager_type==`i' & exp_`j'==1
recode mante`i'_`j' (0=.) if (exp_`j'==. | manager==.)
}
}
save "`path'/computation/V1.dta",replace

// Repr. sample
preserve
keep if ano==2005
keep if nace2>=15 & nace2<=37
collapse (max) manager exporting pemp v_est,by(empresa)
replace manager = 0 if manager==.
collapse (sum) exporting pemp v_est,by(manager)
gen var = 1
reshape wide pemp v_est exporting,i(var) j(manager)
gen r_exp = exporting1/(exporting0+exporting1)
gen r_pes = pemp1/(pemp0+pemp1)
gen r_v_est = v_est21/(v_est20+v_est21)
l
restore

// (V2) Worker-level regressions data set
save "`path'/computation/firm_regressions_exp.dta",replace
keep if ano>=1992  & ano<=2006
sort nss ano
save "`path'/computation/manager_regressions_exp.dta",replace
preserve
keep nss ano
gen var=1
sort nss ano
save "`path'/computation/wage_data1.dta",replace // save worker-year identifiers from this sample - we use this file to make sure we use the same worker-year in the wage regressions with experience in a product
restore

// (V3) Firm-level regressions data set
use "`path'/computation/firm_regressions_exp.dta",clear
// Keep only managers
keep if manager==1
// Mean and Sd (within the firm, over managers with export experience) of age and education (note that variable will be at the firm-level after the collapse)
gen double mean_age_exp = age if exp==1
gen double mean_educ_exp = educ_yrs if exp==1
// Recent experience
gen rexp = exp==1 & yrexp<=3
forvalues i=1/14 {
gen rexp_`i' = exp_`i'==1 & yrexp_`i'<=3
}
local pc  = "311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach h of local pc {
gen rexp_`h' = exp_`h'==1 & yrexp_ang`h'<=3
}
collapse (max) exp exp_* rexp rexp_* mant* (mean) npc_alt exporting v_est ln_firmage foreign sh_skilled sh_skilled_d nace2 n2emp  pemp vendas_euro meanwk_age=age meanwk_educ=educ_yrs mean_age_* mean_educ_exp* de_*   new_exporter cont_exporter exit_exporter never_exporter other_exporter (sd) sdwk_age=age sdwk_educ=educ_yrs sd_age_exp=mean_age_exp sd_educ_exp=mean_educ_exp, by(empresa ano)
// Recode sd to zero if the firm has only one manager with export experience 
replace sdwk_age = 0 if meanwk_age!=. & sdwk_age==.
replace sdwk_educ = 0 if meanwk_educ!=. & sdwk_educ==.
replace sd_age_exp = 0 if sdwk_age!=. & sd_age_exp==.
replace sd_educ_exp = 0 if sdwk_educ!=. & sd_educ_exp==.
replace mean_age_exp = 0 if meanwk_age!=. & mean_age_exp==.
replace mean_educ_exp = 0 if meanwk_educ!=. & mean_educ_exp==.
// Insert data set on firms' exports by destination generated with the dofile destinations.do
sort npc_alt ano
merge npc_alt ano using "`path'/computation/destinations2_ap"
tab ano _merge
keep if _merge==1 | _merge==3
drop _merge
rename v_est_spain v_est_1
rename v_est_ooecd v_est_2
rename v_est_china v_est_3
rename v_est_otop5 v_est_4
rename v_est_excol v_est_5
rename v_est_oeu v_est_6
rename v_est_row v_est_7
rename v_est_ang v_est_8
rename v_est_cv v_est_9
rename v_est_gw v_est_10
rename v_est_st v_est_11
rename v_est_mz v_est_12
rename v_est_br v_est_13
rename v_est_tl v_est_14
local pc  = "311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach h of local pc {
rename v_est_ang`h' v_est_`h'
}
// Reshape
reshape long de_ exp_ rexp_ v_est_ mante1_ mante2_,i(empresa ano) j(dest)

// Manufacturing
keep if nace2>=15 & nace2<=37
save "`path'/computation/firm_regressions_exp.dta",replace


///////************************************************//////////
///////      HERE WE EXPLOIT THE PRODUCT DIMENSION     //////////
///////************************************************//////////


// (C9_gr) Construct workers' export experience in a product group
use "`path'/computation/C8.dta",clear
drop exporting
sort npc_alt ano
merge m:1 npc_alt ano using "`path'/computation/productgr1"
drop exporting
tab ano _merge
keep if _merge==1 | _merge==3
drop _merge
gen byte exporting = v_est2>0 & v_est2!=. // need to compute exporting for 2001 as well
replace exporting = . if v_est2==.
keep ano empresa nss exporting pr_*
compress
reshape wide empresa exporting pr_*,i(nss) j(ano)
local nc = "311 313 314 321 322 323 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 324 999"
local v=1992 
while `v'<=2006 {
local d = 1
while `d'<=`v'-1991 {
local s = `v' - `d'
if `d'==1 {
gen byte iexp`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1   /* dummy 0/1 : worked in other firms, that were exporting, in the past */
gen byte yriexp`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1
foreach i of local nc {
gen byte iexp_`i'_`v' = empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & pr_gr_`i'`s'==1
gen byte yriexp_`i'_`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & pr_gr_`i'`s'==1
}
}
else {
replace iexp`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1
replace yriexp`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1
foreach i of local nc {
replace iexp_`i'_`v' = 1 if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & pr_gr_`i'`s'==1
replace yriexp_`i'_`v' = `d' if empresa`v'!=empresa`s' & empresa`v'!=. & empresa`s'!=. & exporting`s'==1 & pr_gr_`i'`s'==1
}
}
local d = `d' +1
}
local v = `v' +1
}
drop exporting* pr_*
save "`path'/computation/temp.dta", replace
reshape long empresa iexp iexp_311_ iexp_313_ iexp_314_ iexp_321_ iexp_322_ iexp_323_ iexp_324_ iexp_331_ iexp_332_ iexp_341_ iexp_342_ iexp_351_ iexp_352_ iexp_353_ iexp_354_ iexp_355_ iexp_356_ iexp_361_ iexp_362_ iexp_369_ iexp_371_ iexp_372_ iexp_381_ iexp_382_ iexp_383_ iexp_384_ iexp_385_ iexp_390_ iexp_999_ yriexp yriexp_311_ yriexp_313_ yriexp_314_ yriexp_321_ yriexp_322_ yriexp_323_ yriexp_324_ yriexp_331_ yriexp_332_ yriexp_341_ yriexp_342_ yriexp_351_ yriexp_352_ yriexp_353_ yriexp_354_ yriexp_355_ yriexp_356_ yriexp_361_ yriexp_362_ yriexp_369_ yriexp_371_ yriexp_372_ yriexp_381_ yriexp_382_ yriexp_383_ yriexp_384_ yriexp_385_ yriexp_390_ yriexp_999_,i(nss) j(ano)
drop if empresa==.
// Fix issue of workers returning to old firms  
gen double minexpano = ano if iexp==1
egen double minexp=min(minexpano),by(nss)
gen byte exp = (ano>=minexp)
gen byte yrexp = yriexp if ano>=minexp
sort nss ano
replace yrexp = yrexp[_n-1] + 1 if yrexp==. & ano>=minexp
foreach i of local nc {
gen double minexpano_`i' = ano if iexp_`i'_==1
egen double minexp_`i'=min(minexpano_`i'),by(nss)
gen byte exp_`i' = (ano>=minexp_`i')
gen yrexp_`i' = yriexp_`i' if ano>=minexp_`i'
sort nss ano
replace yrexp_`i' = yrexp_`i'[_n-1] + 1 if yrexp_`i'==. & ano>=minexp_`i'
drop iexp_`i'_ minexp_`i' minexpano_`i'
replace exp_`i' = . if exp_`i'==0 & ano==1991
replace yrexp_`i' = . if exp_`i'==0 & ano==1991
}
compress
sort nss ano
save "`path'/computation/experience_gr.dta",replace
use "`path'/computation/C8.dta",clear
sort nss ano
merge 1:1 nss ano using "`path'/computation/experience_gr.dta"
tab _merge
keep if _merge==3
drop _merge
drop if ano==2001 /* drop filled workers */
// Interact manager type with export experience
gen byte mante1 = manager_type==1 & exp==1
gen byte mante2 = manager_type==2 & exp==1
recode mante1 mante2 (0=.) if (exp==. | manager==.)
label var mante1 "Other Manager with experience"
label var mante2 "Sales Manager with experience"
sort empresa ano
save "`path'/computation/C9_gr.dta",replace


// (C10_gr) Size, productivity, and sector of the worker's previous firm
gen double relsize = log(pemp)
gen double relapp_lbprod = log(vendas_euro/pemp)
sort nss ano
gen double prev_firmid = empresa[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=.
sort nss ano
replace prev_firmid = prev_firmid[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] & prev_firmid==.  
sort nss ano
gen double prev_size = relsize[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=.
sort nss ano
replace prev_size = prev_size[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] & prev_size==. 
sort nss ano
gen double prev_prod = relapp_lbprod[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=.
sort nss ano
replace prev_prod = prev_prod[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] &  prev_prod==.
sort nss ano
gen double prev_secs = nace2==nace2[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=. & nace2!=. & nace2[_n-1]!=.
sort nss ano
replace prev_secs = prev_secs[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] &  prev_secs==.
sort nss ano
gen double prev_secd = nace2!=nace2[_n-1] if nss==nss[_n-1] & empresa!=empresa[_n-1] & empresa[_n-1]!=. & nace2!=. & nace2[_n-1]!=.
sort nss ano
replace prev_secd = prev_secd[_n-1] if nss==nss[_n-1] & empresa==empresa[_n-1] &  prev_secd==.
recode prev_secs prev_secd (.=0)
gen byte prev_sec_d = prev_secs==0 & prev_secd==0  // = 1 if missing
sum nss ano prev_size prev_prod prev_sec*
gen byte prev_size_d = prev_size!=.  // = 1 if non-missing
gen byte prev_prod_d = prev_prod!=.  // = 1 if non-missing
recode prev_size prev_prod (.=0) 
drop relsize relapp_lbprod
sum nss ano prev_size* prev_prod* prev_sec*
save "`path'/computation/C10_gr.dta",replace 


// Create Variables to be Used in the Regressions and Save (V)
//------------------------------------------------------------

// V1
// Matched export experience
// matchexp = 1 if the worker has experience in a product group the firm is producing
use "`path'/computation/C10_gr.dta", clear
keep ano npc_alt empresa fem pemp vendas_euro v_est2 nace2 ln_firmage foreign sh_skilled sh_skilled_d nace2 n2emp nss age educ_yrs hwage hwage_norm anoadm ten manager*  new_exporter cont_exporter exit_exporter never_exporter other_exporter exp* yrexp* prev_size prev_prod prev_secs prev_secd prev_size_d prev_prod_d prev_sec_d prev_firmid tot_hours_wk mant*
sort npc_alt ano
merge m:1 npc_alt ano using "`path'/computation/productgr1.dta"
tab ano _merge
keep if _merge==1 | _merge==3
drop _merge
local nc = "311 313 314 321 322 323 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 324 999"
gen byte matchexp = (pr_gr_311==1 & exp_311==1)
foreach i of local nc {
replace matchexp =1 if pr_gr_`i'==1 & exp_`i'==1
}
replace matchexp = . if exp==.
save "`path'/computation/V1_gr.dta", replace
// Interact manager type "i" with export experience in product group "j"
keep if manager_type!=.
keep ano empresa manager_type exp* yrexp*
collapse (max) yrexp* exp* ,by (empresa ano manager_type)
local nc = "311 313 314 321 322 323 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 324 999"
foreach j of local nc {
rename exp_`j' exp_`j'_
rename yrexp_`j' yrexp_`j'_
}
reshape wide exp* yrexp*, i(empresa ano) j(manager_type)
forvalues i=1/2 {
foreach j of local nc {
rename exp_`j'_`i'  mante`i'_`j'
}
}
recode mante* (.=0) 
sort empresa ano 
save mng_int, replace


// (V2) Worker-level regressions data set
use "`path'/computation/V1_gr.dta", clear
save "`path'/computation/firm_regressions_exp_gr.dta",replace
keep if ano>=1992  & ano<=2006
sort nss ano
save "`path'/computation/manager_regressions_exp_gr.dta",replace
preserve
keep nss ano
gen var=1
sort nss ano
save "`path'/computation/wage_data2.dta",replace  // save worker-year identifiers from this sample - we use this file to make sure we use the same worker-year in the wage regressions with experience in a destination
restore


// (V3) Firm-level regressions data set
use "`path'/computation/firm_regressions_exp_gr.dta",clear
// Keep only managers
keep if manager==1
// Mean and Sd (within the firm, over managers with export experience) of age and education (note that variable will be at the firm-level after the collapse)
gen double mean_age_exp = age if exp==1
gen double mean_educ_exp = educ_yrs if exp==1
// Recent experience
gen rexp = exp==1 & yrexp<=3
local pc  = "311 313 314 321 322 323 324 331 332 341 342 351 352 353 354 355 356 361 362 369 371 372 381 382 383 384 385 390 999"
foreach h of local pc {
gen rexp_`h' = exp_`h'==1 & yrexp_`h'<=3
}

collapse (max) exp exp_* rexp* mant* (mean) npc_alt exporting v_est ln_firmage foreign sh_skilled sh_skilled_d nace2 n2emp pemp vendas_euro meanwk_age=age meanwk_educ=educ_yrs mean_age_* mean_educ_exp* pr_gr_*   new_exporter cont_exporter exit_exporter never_exporter other_exporter (sd) sdwk_age=age sdwk_educ=educ_yrs sd_age_exp=mean_age_exp sd_educ_exp=mean_educ_exp, by(empresa ano)

// Recode sd to zero if the firm has only one manager with export experience 
replace sdwk_age = 0 if meanwk_age!=. & sdwk_age==.
replace sdwk_educ = 0 if meanwk_educ!=. & sdwk_educ==.
replace sd_age_exp = 0 if sdwk_age!=. & sd_age_exp==.
replace sd_educ_exp = 0 if sdwk_educ!=. & sd_educ_exp==.
replace mean_age_exp = 0 if meanwk_age!=. & mean_age_exp==.
replace mean_educ_exp = 0 if meanwk_educ!=. & mean_educ_exp==.
// Insert data set on firms' exports by product group generated with the dofile product.do
sort npc_alt ano
merge npc_alt ano using "`path'/computation/productgr2"
tab ano _merge
keep if _merge==1 | _merge==3
drop _merge

// Reshape
reshape long pr_gr_ exp_ rexp_ v_est_pr_gr_ mante1_ mante2_,i(empresa ano) j(pr_gr)

// Manufacturing
keep if nace2>=15 & nace2<=37
save "`path'/computation/firm_regressions_exp_gr.dta", replace


log close
